import java.awt.event.KeyEvent;

import org.powerbot.core.script.job.state.Node;
import org.powerbot.game.api.methods.Game;
import org.powerbot.game.api.methods.Widgets;
import org.powerbot.game.api.methods.input.Keyboard;
import org.powerbot.game.api.wrappers.widget.WidgetChild;


public class LoginManager extends Node{

	public static String user, pass, status="idle";
	public static int state = 0;
	public static boolean ready = false;
	public static void Set(String a, String b)
	{
		user = a;
		pass = b;
		ready = true;
	}
	@Override
	public void execute()
	{
			switch(Game.getClientState())
			{
			case Game.INDEX_MAP_LOADED:
				status = "Loaded map";
				break;
			case Game.INDEX_LOGGING_IN:
				status = "Logging in..";
				break;
			case Game.INDEX_LOGIN_SCREEN:
			{
				WidgetChild userField = Widgets.get(596).getChild(39),
							passField = Widgets.get(596).getChild(41);
				
				status = "logging into "+user;
				userField.click(true);
				if(!userField.getText().toLowerCase().equals(user))
				{
					if(userField.getText().length() != 0)
						for(int x=0;x<=userField.getText().length();x++)
							Keyboard.sendKey((char)KeyEvent.VK_BACK_SPACE);
					else
						Keyboard.sendText(user, false);
				}
				
				passField.click(true);
				if(passField.getText().length() != 0 )
					for(int x=0;x<=passField.getText().length();x++)
						Keyboard.sendKey((char)KeyEvent.VK_BACK_SPACE);
				else
					Keyboard.sendText(pass, true);
				userField = null;
				passField = null;
				break;
			}
			case Game.INDEX_LOBBY_SCREEN:
				status = "in lobby";
				Widgets.get(906).getChild(186).click(true);
				break;
			case 101://already logged in
				status = "already logged in";
				break;
			case 102://invalid details
				status = "invalid details";
				break;
			}
		
	}
	@Override
	public boolean activate() {
		// TODO Auto-generated method stub
		return Game.getClientState() != Game.INDEX_MAP_LOADED;
	}
}
